package com.example.directionsensor;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class directorsensor extends AppCompatActivity implements SensorEventListener {
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private SensorManager sensorManager;
    private Sensor sensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_directorsensor);

        textView1= (TextView) findViewById(R.id.activity_sensormanager_textview1);
        textView2= (TextView) findViewById(R.id.activity_sensormanager_textview2);
        textView3= (TextView) findViewById(R.id.activity_sensormanager_textview3);

        sensorManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
         //获取Sensor实例

    }

    public void onResume()
    {
        super.onResume();
        //为系统的加速度传感器注册监听器
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
    }

    public void onPause()
    {
        super.onPause();
        //取消注册
        sensorManager.unregisterListener(this);
    }



    @Override
    public void onSensorChanged(SensorEvent event) {
        textView1.setText("方位角：" + (float) (Math.round(event.values[0] * 100)) / 100);
        textView2.setText("倾斜角：" + (float) (Math.round(event.values[1] * 100)) / 100);
        textView3.setText("滚动角：" + (float) (Math.round(event.values[2] * 100)) / 100);


    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
